package com.example.huichaoliu;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.huichaoliu.adapters.ActivityShareAdapter;
import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.huichaoliu.data.UserInfo;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.rederxu.tools.StringUtils;
import com.source.utils.Utils;

public class PreferInputActivity extends BaseActivity implements OnClickListener {

	private EditText et_name = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_prefer_input);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		case R.id.btn_save:
			String name = et_name.getText().toString();
			if(StringUtils.isBlank(name) && name.length() > 1024){
				showToast("爱好输入不合法");
				return;
			}
			try{
				JSONObject jo = new JSONObject();
				jo.put("hobby", name);
				jo.put("userId", Config.getUserId(this));
				Bundle bundle = new Bundle();
				bundle.putString("request", Utils.requestParamsEncode(jo));
				HTTPRequest.POST(this, Config.REQUEST_URLS.URL_USER_UPDATE_INFO, bundle, new onRequestOverListener() {
					@Override
					public void onRequestOver(String result) {
						try{
							JSONObject jo = new JSONObject(result);
							if(jo.getInt("code") != 0){
								showToast(jo.getString("message"));
								return;
							}else{
								showToast(jo.getString("message"));
								jo = jo.getJSONObject("response");
								UserInfo.initData(jo, PreferInputActivity.this);
								setResult(RESULT_OK);
								finish();
								return;
							}
						}catch(JSONException e){
							e.printStackTrace();
						}
					}
				});
			}catch(Exception e){
				e.printStackTrace();
			}
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("修改爱好");
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.btn_save).setOnClickListener(this);
		et_name = (EditText) findViewById(R.id.et_name);
	}

	@Override
	public void initData() {
		
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

}
